// pages/lottery/lottery.js

const net = require('../../utils/request.js');
var animation
var hassign ;//是否签到
var hasinvest ;//是否出借
var sign_receive ;//是否领取签到次数
var invest_receive;//是否领取出借次数
var valHandle;  //答题定时器

var prize;//奖品类型
var img;//实物奖品的图片
var show_name;//实物奖品的名字
var introduce;//实物奖品的兑奖说明
Page({

  /**
   * 页面的初始数据
   */
  data: {
    animationData: {},
    signText:'去签到',
    investText:'去出借',
    isChanged:true,
    surplusNum:0,//剩余次数
    prizeImageList:[],
    preCircle:0,
    prePrize:0,
    isAnimation:false,
    explain:'', //帮助弹窗内容
    showLoginDialog:false,//登录弹窗
    shareLottery:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

     animation = wx.createAnimation({
      duration: 3000,
      timingFunction: 'ease-in-out'
    });
    this.data.shareLottery = options.share;
 
    var that = this;

    valHandle = setInterval(function () {
      that.setData({
        isChanged:!that.isChanged 
      })
      that.isChanged=!that.isChanged ;

    }, 1000)
    this.isLogin();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.requestPageData();
    //是否登录
    this.isLogin();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    wx.showShareMenu({
      withShareTicket: true
    })
    return {
      title: '送你的免费抽奖机会，不要错过哦！',
      path: 'pages/lottery/lottery?share=lottery',
      imageUrl: '',
    }
  },

  lotteryStart:function(e){
    var count = e;
    var circle =  6 - this.data.prePrize;
    this.data.prePrize = count;
    this.data.preCircle = -((count + 18 + circle) * 60) + this.data.preCircle;
    animation.rotate(this.data.preCircle).step();
    this.setData({
      animationData: animation.export()
    });
  },

  //跳转我的奖品
  jumpPrize:function(){
    this.isLogin();
    if (!this.data.showLoginDialog) {
      wx.navigateTo({
        url: '../MyPrize/MyPrize',
      })
    }
  
  },

  //点击帮助
  clickHelp:function(){
    // this.requestLotteryDraw();
    this.HelpDialog = this.selectComponent("#HelpDialog");
    this.HelpDialog.showDialog();
  },

  //点击活动未开始返回按钮
  _goBack:function(){
    this.ActionOpenDialog = this.selectComponent("#ActionOpenDialog");
    this.ActionOpenDialog.hiddenDialog();
    // wx.navigateBack({
      
    // })
    if (this.data.shareLottery) {
      wx.redirectTo({
        url: '../index/index',
      })
    } else {
      wx.navigateBack({
      });
    }
    
  },

  //点击福利按钮
  popHomeRoot: function () {
    if(this.data.shareLottery)
    {
      wx.redirectTo({
        url: '../index/index',
      })
    }else
    {
      wx.navigateBack({
      });
    }
   
  },

  // 登录弹窗点击事件
  _notNowEvent:function(){
    this.setData({
      showLoginDialog: false
    })
  },

  _logoEvent:function(){
    // channel为1是抽奖
    wx.navigateTo({
      url: '../logs/logs?channel=1',
    })
  },

//请求页面初始化的数据
  requestPageData:function(){
    var that=this;
    that.GetNum = that.selectComponent("#GetNum");
    net.requestPostHttp("welfare/lottery/event",{},function(res){
      //圆点设置  
      console.log(res);
      console.log(res.data.state)
      //res.data.state为1的话表示活动开启,否则活动结束
      if (res.data.data.eventstart==1){
        that.ActionOpenDialog = that.selectComponent("#ActionOpenDialog");
        that.ActionOpenDialog.hiddenDialog();
      }else{
        that.ActionOpenDialog = that.selectComponent("#ActionOpenDialog");
        that.ActionOpenDialog.showDialog();
      }
      var circleList = [];

      let image1 = res.data.data.img4;
      var circle = ((60) * 0);
      circleList.push({ circle: circle,imageUrl: image1, prize:4})
     
      let image2 = res.data.data.img1;
      circle = ((60) * 1);
      circleList.push({ circle: circle, imageUrl: image2, prize: 1 })


      let image3 = res.data.data.img2;
      circle = ((60) * 2);
      circleList.push({ circle: circle, imageUrl: image3, prize: 2 })

      let image4 = res.data.data.img5;
      circle = ((60) * 3);
      circleList.push({ circle: circle, imageUrl: image4, prize: 5 })

      let image5 = res.data.data.img3;
      circle = ((60) * 4);
      circleList.push({ circle: circle, imageUrl: image5, prize: 3 })

      let image6 = res.data.data.img6;
      circle = ((60) * 5);
      circleList.push({ circle: circle, imageUrl: image6, prize: 6 })
      that.prizeImageList = circleList;
      that.setData({
        prizeImageList: circleList
      }) 

      that.setData({
        surplusNum: res.data.data.count,//设置剩余次数
        explain: res.data.data.help
      })
       hassign = res.data.data.hassign
       hasinvest = res.data.data.hasinvest
       sign_receive = res.data.data.sign_receive
       invest_receive = res.data.data.invest_receive
      if (hassign==1){
        that.setData({
          signText:"领取"
        })
      }else{
        that.setData({
          signText: "去签到"
        })
      }
      if (hasinvest==1){
        that.setData({
          investText: "领取"
        })
      }else{
        that.setData({
          investText: "去出借"
        })
      }
      if (sign_receive==1){
        that.GetNum.changeSignBackground()
        that.setData({
          signText: "已领取"
        })
      }
      if (invest_receive==1){
        that.GetNum.changeInvestBackground();
        that.setData({
          investText: "已领取"
        })
      }
    })
  },

//获取次数按钮
  GetNumber:function(){
    this.isLogin();
    console.log(this.data.showLoginDialog)
    if (!this.data.showLoginDialog){
      this.GetNum = this.selectComponent("#GetNum");
      this.GetNum.showDialog();
    }  

  },

  //关闭弹窗
  _close_alert: function () {
    this.GetNum = this.selectComponent("#GetNum");
    this.GetNum.hiddenDialog();
  },

  //关闭帮助弹窗
  _close_help_diaolg:function(){
    this.HelpDialog = this.selectComponent("#HelpDialog");
    this.HelpDialog.hiddenDialog();
  },

//关闭领取转盘次数成功弹窗
  _close_num_diaolg:function(){
    this.ReceiveSuccess = this.selectComponent("#ReceiveSuccess");
    this.ReceiveSuccess.hiddenDialog();
  },

  //抽奖弹窗按钮
  _checkPrize:function(){
    var that=this;
    that.PrizeDialog = that.selectComponent("#PrizeDialog");
    if(prize==1||prize==2||prize==3||prize==6){
      that.PrizeDialog.hiddenDialog();
    }else if(prize==4||prize==5){
      wx.navigateTo({
        url: "/pages/MyPrize/PrizeDetail?introduce=" + encodeURIComponent(introduce) + "&show_name=" + show_name + "&pic=" + img,
      })
      that.PrizeDialog.hiddenDialog();
    }
  },

  //点击签到或领取按钮
  _signClick:function(){
    //如果已经签到点击按钮就去请求领取次数接口否则跳转首页去签到
    if (hassign==1){
      this.getNumRequest(1);
    }else{
      if (this.data.shareLottery) {
        wx.redirectTo({
          url: '../index/index',
        })
      } else {
        wx.navigateBack({
        });
      }
      // wx.navigateBack({
      //   // url: '../index/index',
      // })
    }
   
  },
  //点击去出借或领取按钮
  _investClick:function(){
    //如果已经签到点击按钮就去请求领取次数接口否则跳转为微站投资
    if (hasinvest==1){
      this.getNumRequest(2);
    }else{
      wx.navigateTo({
        url: '/pages/webViewPage/Webview?url=https://m.yijiedai.com',
      })
    }
   
  },
  //点击领取按钮请求接口
  getNumRequest:function(type){
    var that=this;
    that.GetNum = that.selectComponent("#GetNum");
    that.ReceiveSuccess = that.selectComponent("#ReceiveSuccess");
    var param={
      type:type
    }
    net.requestPostHttp('welfare/lottery/addCount', param,function(res){
      console.log(res.data.msg)
      console.log(res.data.data.count)
      console.log(res.data.count)
      if(type==1&&res.data.msg=="领取成功"){
        that.GetNum.changeSignBackground();
        that.ReceiveSuccess.showDialog();
        that.setData({
          surplusNum:res.data.data.count
        })
      }
      if (type == 2 && res.data.msg == "领取成功"){
        that.GetNum.changeInvestBackground();
        that.ReceiveSuccess.showDialog();
        that.setData({
          surplusNum: res.data.data.count
        })
      }
    })
  },

  //请求抽奖接口
  requestLotteryDraw:function(){
    this.isLogin()
    //如果是登录状态才开始执行抽奖
    if (!this.data.showLoginDialog) {
      if (this.data.surplusNum <= 0) {
        wx.showToast({
          title: '今日抽奖次数已用完',
          icon: 'none', //图标，支持"success"、"loading"  

        })
        return;
      }
      
      if (this.isAnimation) {
        return;
      }
      var that = this;
      that.PrizeDialog = that.selectComponent("#PrizeDialog");
      net.requestPostHttp("welfare/lottery/receive", {}, function (res) {
        console.log(res);
        that.isAnimation = true;
        if (res.data.state != '1') return;

        console.log(res.data.data.count)
        that.setData({
          surplusNum: res.data.data.count
        })
        //获取奖品的类型
         prize = res.data.data.prize
        //代金券金额
       var  price = parseInt(res.data.data.price)
        //实物奖品的图片
         img = res.data.data.img
        //实物奖品的名字
         show_name = res.data.data.show_name;
        //实物奖品的兑奖说明
         introduce = res.data.data.introduce
        console.log(prize);

        var count = 0;
        for (count; count < 6; count++) {
          var prizeDic = that.prizeImageList[count];
          if (prizeDic.prize == prize) {
            break;
          }
        };
        that.lotteryStart(count)

        //抽奖弹窗

        setTimeout(function () {
          that.PrizeDialog.showDialog()
          if (prize) {
            that.PrizeDialog.changeImg(prize, price, img, show_name)
          }
          that.isAnimation = false;
        }, 3500)
      })
    }
    
  },

  //判断用户是否登录
  isLogin:function(){
    var that=this
  var userInfo= wx.getStorageSync('userInfo');
  console.log(userInfo)
    if (userInfo){
      that.setData({
        showLoginDialog:false
      })
    }else{
      that.setData({
        showLoginDialog: true
      })
    }
  }
})